<?php
/*
 * @Date: 2021-05-06 21:25:22
 * @Author: yukun
 * @LastEditors: yukun
 * @LastEditTime: 2021-05-06 21:45:07
 * @FilePath: /design-patterns-study/工厂模式/FactorySimple.php
 * @Description: 简单工厂模式
 */

interface db
{
    public function connect();
}

class mysql implements db
{
    public function connect()
    {
        echo "已经连接了MySQL" . PHP_EOL;
    }
}

class sqlite implements db
{
    public function connect()
    {
        echo "已经连接了sqlite" . PHP_EOL;
    }
}

class DBFactory
{
    public static function getDB(string $type)
    {
        switch ($type) {
            case 'mysql':
                return new mysql();
                break;
            case 'sqlite':
                return new sqlite();
                break;
            default:
                throw new Exception("错误的数据库类型", 1);
                break;
        }
    }
}

// 简单工厂方法
// 当需要增加一种新的数据库类型，则需要改动工厂方法getDB方法

$db = DBFactory::getDB('mysql');
$db->connect();


$db = DBFactory::getDB('sqlite');
$db->connect();
